﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using RouterControl.Web;
using RouterControl.Db;
using System.IO;

// NOTE: If you change the class name "RouterControlWS2" here, you must also update the reference to "RouterControlWS2" in Web.config.
public class RouterControlWS2 : IRouterControlWS2
{
    private DeviceDomDbConnector _db = new DeviceDomDbConnector();

    public void UploadRouterDescription(Stream xmlStream)
    {
        if (xmlStream == null)
	        throw new ArgumentNullException("xmlStream");

        throw new NotSupportedException("feature is currently disabled");
        /* TODO: implement device dom loading support
         * 
        // load xml
        var trans = DeviceSchemaDomTransform.CreateFromSchemaXml(xmlStream);

        // add
        _db.SaveDeviceSchema(trans);*/
    }

    public bool HasRouterDescription(string routerUniqueId)
    {
        if (string.IsNullOrEmpty(routerUniqueId))
            throw new ArgumentException("null or empty", "routerUniqueId");

        return _db.HasDeviceSchema(routerUniqueId);
    }

    public bool HasRouterDescriptionEx(string routerUniqueId, int versionId)
    {
        if (string.IsNullOrEmpty(routerUniqueId))
            throw new ArgumentException("null or empty", "routerUniqueId");

        return _db.HasDeviceSchema(routerUniqueId, versionId);
    }

    public Stream GetMaxSpecDeviceDom(string rootDeviceTypeId)
    {
        if (string.IsNullOrEmpty(rootDeviceTypeId))
            throw new ArgumentException("null or empty", "rootDeviceTypeId");

        throw new NotSupportedException("feature is currently disabled");
        /* TODO: implement device dom loading support

        // retrieve
        var schemaId = MaxSpecDom.GetSchemaId(rootDeviceTypeId);

        var dom = _db.LoadDeviceDom(schemaId);

        // handle if not found
        if (dom == null)
            return null;

        // serialize
        var ms = new MemoryStream(64 * 1024);  // 64KB

        dom.ToXml(ms);

        ms.Position = 0;

        return ms;*/
    }

    public string GetNewerVersion(int versionId)
    {
        var latest = _db.GetLatesRelease(true, false);

        _db.LogUpdateCheck(
            Guid.Empty,
            versionId,
            latest.ReleaseId,
            latest.VersionStr
            );

        if (versionId < latest.ReleaseId)
            return latest.VersionStr;

        return string.Empty;
    }

    public ReleaseInfo GetNewerVersion2(int versionId)
    {
        var latest = _db.GetLatesRelease(true, false);

        _db.LogUpdateCheck(
            Guid.Empty,
            versionId,
            latest.ReleaseId,
            latest.VersionStr
            );

        return latest.ToWeb();
    }

    public ReleaseInfo GetNewerVersion3(string instanceId, int versionId, bool includeBetaRelease)
    {
        var latest = _db.GetLatesRelease(true, includeBetaRelease);

        _db.LogUpdateCheck(
            new Guid(instanceId),
            versionId,
            latest.ReleaseId,
            latest.VersionStr
            );

        return latest.ToWeb();
    }

    public void ReportError(ErrorInfo error)
    {
        _db.AddErrorReport(
            error.ToDatabase()
            );
    }

    public void UploadUsageInfo(UsageInfo info)
    {
        _db.UploadUsageInfo(info);
    }
}
